package com.qdlc.p2p.biz.service.activity.cashaward;

import javax.annotation.Resource;

import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.CashAwardRuleService;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.biz.service.activity.AbstractActivityProgrammeHandler;
import com.qdlc.p2p.biz.service.activity.ActivityPlanException;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeEnum;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeParam;
import com.qdlc.p2p.biz.service.activity.param.ActivityProgrammeCashAwardParam;
import com.qdlc.p2p.common.constant.CashAwardType;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.dal.dao.NewActivityAwardLogDao;
import com.qdlc.p2p.dal.dto.NewActivityAwardLog;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.CashAwardRuleModel;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 活动：返现奖励
 *
 * @author linya
 */
@Service
public class ActivityProgrammeCashAwardHandler extends AbstractActivityProgrammeHandler<ActivityProgrammeParam> {

    @Resource
    CashAwardRuleService cashAwardRuleService;

    @Resource
    NewActivityAwardLogDao newActivityAwardLogDao;

    @Resource
    @Qualifier("cashAwardCollectServiceImpl")
    CollectPayService cashAwardCollectServiceImpl;

    @Resource
    BorrowTenderService borrowTenderService;

    @Override
    public ActivityProgrammeEnum supportsProgramme() {
        return ActivityProgrammeEnum.CASH_AWARD;
    }

    @Override
    public ActivityProgrammeCashAwardParam createProgrammeParam(Object param) {
        ActivityProgrammeCashAwardParam cashAwardParam;
        if (param instanceof ActivityProgrammeCashAwardParam) {
            cashAwardParam = (ActivityProgrammeCashAwardParam) param;
        } else if (param instanceof Double) {
            cashAwardParam = new ActivityProgrammeCashAwardParam();
            cashAwardParam.setAmount((Double) param);
        } else {
            throw new IllegalArgumentException("ActivityProgrammeCashAwardHandler's attatchmentObject type must to be ActivityProgrammeCashAwardParam or Double!");
        }
        return cashAwardParam;
    }

    /**
     * 发现处理，发起代付请求
     *
     * @param awardAmount
     * @param cashAwardParam
     */
    private void doCashAward(double awardAmount, ActivityProgrammeParam cashAwardParam) {

        User user = cashAwardParam.getUser();

        //创建订单
        if (awardAmount != 0) {
            // 创建代收订单
            TppTradeLog tppTradeLog = cashAwardCollectServiceImpl.createOrder(user.getUserId(), awardAmount);
            cashAwardCollectServiceImpl.requestOrder(tppTradeLog);
        }
        //记录日志
        String planEnTache = cashAwardParam.getActivityPlan().getEnTache();
        int programmeType = cashAwardParam.getRuleType();
        NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog(user, planEnTache, awardAmount, programmeType);
        newActivityAwardLogDao.save(newActivityAwardLog);
    }

    @Override
    protected void handleInternal(ActivityProgrammeParam param) throws ActivityPlanException {
        ActivityProgrammeCashAwardParam cashAwardParam = this.createProgrammeParam(param);
        Double amount = cashAwardParam.getAmount();
        if (amount == null) {
            return;
        }

        //获取返现活动记录
        CashAwardRuleModel cashAward = cashAwardRuleService.findCashAwardRuleById(param.getRuleId());
        if (cashAward == null) {
            return;
        }

        //检测投资金额，只有大于活动限制才能获取返现活动奖金
        double investAmount = cashAward.getInvestAmount();
        if (amount < investAmount) {
            return;
        }

        if (cashAward.getType()==CashAwardType.FIXED_AMOUNT&&cashAward.getMoney() <= 0) {
            return;
        }

        if (cashAward.getType()==CashAwardType.PROPORTIONAL_RETURN&&cashAward.getCashBackRate() <= 0) {
            return;
        }

        double cumulativeAmount  = cashAward.getCumulativeAmount();

        if(cumulativeAmount > 0 ) {
            double investAmont = borrowTenderService.investAmount(param.getUser().getUserId());
            if(investAmont < cumulativeAmount){
                return;
            }
        }

        //奖励资金
        double awardAmount = 0D;

        if(cashAward.getType() == CashAwardType.PROPORTIONAL_RETURN){
            awardAmount = BigDecimalUtil.round(BigDecimalUtil.mul(amount, cashAward.getCashBackRate()));
        }else {
            awardAmount = cashAward .getMoney();
        }

        //返现奖励处理
        doCashAward(awardAmount, param);
    }

    @Override
    public void handleCustom(ActivityProgrammeParam param) throws ActivityPlanException {
        ActivityProgrammeCashAwardParam cashAwardParam = this.createProgrammeParam(param);
        double amount = cashAwardParam.getAmount();
        if (amount != 0D) {
            doCashAward(amount, cashAwardParam);
        }
    }

}
